<?php
namespace Mobile\Controller;
use Think\Controller;

class DetailController extends Controller {
    /**
    * 获取系统通知列表
    * 
    */
    public function index($id){
        /* 标识正确性检测 */
        if(!($id && is_numeric($id))){
            $this->error('文档ID错误！');
        }

        /* 获取详细信息 */
        $Document = D('Document');
        $info = $Document->detail($id);
        if(!$info){
            $this->error($Document->getError());
        }

        /* 更新浏览数 */
        $map = array('id' => $id);
        $Document->where($map)->setInc('view');

        /* 模板赋值并渲染模板 */
        $this->assign('info', $info);
        $this->display();
    }
    
    /**
    * 收益规则
    * 
    */
    public function rules(){
        $category = $this->category();
        
        $Document = D('Document');
        $map['name'] = 'rules';
        $map['category_id'] = $category['id'];
        $map['status'] = 1;
        $doc = $Document->where($map)->find();
        if($doc){
            $docDetail = $Document->detail($doc['id']);
            $this->assign('info',$docDetail);
            $this->display('index');
        }else{
            $this->error('文档不存在或已禁用！');
        }    
    }
    
    /**
    * 购险规则
    * 
    */
    public function insurance(){
        $category = $this->category();
        
        $Document = D('Document');
        $map['name'] = 'insurance';
        $map['category_id'] = $category['id'];
        $map['status'] = 1;
        $doc = $Document->where($map)->find();
        if($doc){
            $docDetail = $Document->detail($doc['id']);
            $this->assign('info',$docDetail);
            $this->display('index');
        }else{
            $this->error('文档不存在或已禁用！');
        }    
    }
    
    /**
    * 智驾协议
    * 
    */
    public function protocol(){
        $category = $this->category();
        
        $Document = D('Document');
        $map['name'] = 'protocol';
        $map['category_id'] = $category['id'];
        $map['status'] = 1;
        $doc = $Document->where($map)->find();
        if($doc){
            $docDetail = $Document->detail($doc['id']);
            $this->assign('info',$docDetail);
            $this->display('index');
        }else{
            $this->error('文档不存在或已禁用！');
        }    
    }
    
    /**
    * 奖励规则
    * 
    */
    public function rewardDetail(){
        $category = $this->category();
        
        $Document = D('Document');
        $map['name'] = 'reward_detail';
        $map['category_id'] = $category['id'];
        $map['status'] = 1;
        $doc = $Document->where($map)->find();
        if($doc){
            $docDetail = $Document->detail($doc['id']);
            $this->assign('info',$docDetail);
            $this->display('index');
        }else{
            $this->error('文档不存在或已禁用！');
        }    
    }
    
    /**
    * 险种介绍
    * 
    */
    public function insuranceDetail(){
        $category = $this->category();
        
        $Document = D('Document');
        $map['name'] = 'types_of_insurance';
        $map['category_id'] = $category['id'];
        $map['status'] = 1;
        $doc = $Document->where($map)->find();
        if($doc){
            $docDetail = $Document->detail($doc['id']);
            $this->assign('info',$docDetail);
            $this->display('index');
        }else{
            $this->error('文档不存在或已禁用！');
        }    
    }
    
    protected function category(){
        $category = D('Category')->info('page');
        if($category && 1 == $category['status']){
            switch ($category['display']) {
                case 0:
                    $this->error('该分类禁止显示！');
                    break;
                default:
                    break;
            }
        } else {
            $this->error('分类不存在或被禁用！');
        }
        return $category;    
    }
}